<?php

class Default_IndexController extends Zend_Controller_Action
{
    public $groups;
    public function init()
    { 
        Zend_Session::start();
        $Session = new Zend_Session_Namespace ('Params');
        unset ($Session->Type);
        unset ($Session->Search);
        unset ($Session->Saved);
        $this->view->Persisted   = $Session->Persisted; 
      #  unset ($Session->Persisted);
    }

    public function indexAction()
    {
      

        $this->_helper->layout->setLayout('layout');
        $g = new Default_Model_UserProfile; 

        $request    = $this->getRequest(); 
        $pagenumber = $request->getParam('page');  

        if (!isset($pagenumber)) $pagenumber = 1;
        $startIndex = ($pagenumber - 1) * 6; 

        $this->view->size      = sizeof($g->groups);
        $this->view->page      = $pagenumber;

        $sliced = array_slice ($g->groups, $startIndex, 6);
        $groups = array();
        foreach ($sliced as $name) {
            $n = new Default_Model_Newsgroup;  
            $n->Name = $name; 
            $groups[] = $n;
        }

        $index      = rand ( 0 , sizeof($groups) - 1 ); 
        $random     = $groups[$index]->GetRandomArticle();
        $randomName = $groups[$index]->Name;
        $limit      = 0;

        while (++$limit < 10 && !$random) {
            $index      =  rand ( 0 , sizeof($groups) - 1 ); 
            $random     = $groups[$index]->GetRandomArticle();
            $randomName = $groups[$index]->Name;
        }

        $this->view->randomName   = $randomName; 
        $this->view->random       = $random; 
        $this->view->groups       = $groups; 
        $this->view->fullList     = $g->groups; 
    }

    public function searchAction()
    {
        $g = new Default_Model_NewsServer; 

        $request    = $this->getRequest(); 
        $pagenumber = $request->getParam('page');  
        $findParam  = $request->getParam('param');  
        if (!isset($pagenumber)) $pagenumber = 1;
        $startIndex = ($pagenumber - 1) * 10;

        if (isset($findParam)) {  
            $g->GetGroups($findParam);
            $sliced = array_slice ($g->groups, $startIndex, 10);
            $this->view->groups = $sliced; 
            $this->view->size   = sizeof($g->groups);  
            $this->view->param  = $findParam; 
            $this->view->page   = $pagenumber; 
        }
    }


}

